﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class QuestLog : MonoBehaviour
{
    
    private static QuestLog instance;
    public static QuestLog MyInstance
    {
        get
        {
            if (instance == null)
            {
                instance = FindObjectOfType<QuestLog>();
            }
            return instance;
        }
    }
    private List<QuestScript> questScripts = new List<QuestScript>();
    [SerializeField]
    private Transform questList;
    [SerializeField]
    private GameObject questPrefab;
    [SerializeField]
    private Text descTxt;
    private Quest selected;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    public void Accept(Quest q)
    {
        foreach(CollectObjective co in q.MyCollectObjectives)
        {
            inventoryScript.MyInstance.itemCountEvent+=new ItemCount(co.UpdateItemCount);
        }
        GameObject gameObject = Instantiate(questPrefab, questList);
        QuestScript qs= gameObject.GetComponent<QuestScript>();
        qs.MyQuest = q;
        qs.MyQuest.MyQuestScript = qs;
        gameObject.GetComponentInChildren<Text>().text = q.MyTitle;
        questScripts.Add(qs);
    }
    public void ShowDesc(Quest q)
    {
        if (q != null)
        {
            if (selected != null && q!= selected )
            {
                selected.MyQuestScript.Deselect();    
            }
            string objectives = "\nObjectives:\n";
            foreach (Objective o in q.MyCollectObjectives)
            {
                objectives += "• " + o.MyType + ": " + o.MyCurrentAmount + "/" + o.MyAmount + "\n";
            }
            selected = q;
            descTxt.text = string.Format("<size=14>{0}</size> \n\n {1} {2}",selected.MyTitle,selected.MyQLDescription,objectives);
        }
    }
    public void UpdateCount()
    {
        ShowDesc(selected);

    }
    public void CheckCompletion()
    {
        foreach(QuestScript qs in questScripts)
        {
            qs.QuestDone();
        }
    }
}
